//
//  BLMNSArray.m
//  IOSDome
//
//  Created by 刘波 on 2019/5/9.
//  Copyright © 2019 clobe. All rights reserved.
//

#import "XCNSArray.h"

@implementation XCNSArray

/**
 数组的基本用法
 */
-(void)setNSArray{
    
    
    //数组的基本用法
    
    NSArray *array;
    //创建一个空素组
    array = [[NSArray alloc] init];
    array = [NSArray array];
    array = [NSArray new];
    array = @[];
    //通过指定对象创建数组
    array = [NSArray arrayWithObject:@"a"];
    array = [NSArray arrayWithObjects:@"a",@"b",@"c", nil];
    array = @[@"a",@"b",@"c"];
    //通过数组创建数组
    NSArray *array1 = [NSArray arrayWithArray:array];
    //把数组转换成字符串，两个元素之间用逗号分隔
    NSString *str = [array componentsJoinedByString:@","];
    //获取数组中元素的个数
    NSInteger num = [array count];
    //通过下标获取元素，注意下标不要大于或等于数组的count
    NSString *item;
    item = [array objectAtIndex:0];
    item = array[0];
    item = [array firstObject]; //获取数组第一个元素
    item = [array lastObject];  //获取数组最后一个元素
    //判断数组是否包含某个元素
    BOOL isContains = [array containsObject:@"a"];
    //取出数组一部分元素
    NSRange range = NSMakeRange(0, 3);
    NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:range];
    NSArray *array2;
    array2 = [array objectsAtIndexes:indexSet];
    array2 = [array subarrayWithRange:range];
    
    //可变数组 新增用法

    NSMutableArray *mArray = [[NSMutableArray alloc] init];
    //增加一个元素
    [mArray addObject:@"a"];
    //增加一个数组
    [mArray addObjectsFromArray:array];
    //根据下标插入一个元素
    [mArray insertObject:@"b" atIndex:1];
    //根据下标删除一个元素
    [mArray removeObjectAtIndex:1];
    //根据对象删除元素
    [mArray removeObject:@"a"];
    //删除所有元素
    [mArray removeAllObjects];
    //根据下标替换元素
    [mArray replaceObjectAtIndex:1 withObject:@"b"];
    //根据下标交换两个元素
    [mArray exchangeObjectAtIndex:2 withObjectAtIndex:1];
    
    
}

@end
